package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.system.controller.CxBase;
import com.ruoyi.system.domain.CooperativeMerchant;
import com.ruoyi.system.mapper.CooperativeMerchantMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AdvertisementInfoMapper;
import com.ruoyi.system.domain.AdvertisementInfo;
import com.ruoyi.system.service.IAdvertisementInfoService;

/**
 * 广告信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-07
 */
@Service
public class AdvertisementInfoServiceImpl extends CxBase implements IAdvertisementInfoService
{
    @Autowired
    private AdvertisementInfoMapper advertisementInfoMapper;
    @Autowired
    private CooperativeMerchantMapper cooperativeMerchantMapper;

    /**
     * 查询广告信息
     * 
     * @param advertisementId 广告信息ID
     * @return 广告信息
     */
    @Override
    public AdvertisementInfo selectAdvertisementInfoById(String advertisementId)
    {
        if (!checkNull(advertisementId)){
            AdvertisementInfo advertisementInfo = advertisementInfoMapper.selectAdvertisementInfoById(advertisementId);
            advertisementInfo.setSetDate(StampToString(Long.valueOf(advertisementInfo.getSetDate())));
            return advertisementInfo;
        }
        return null;
    }

    /**
     * 查询广告信息列表
     * 
     * @param advertisementInfo 广告信息
     * @return 广告信息
     */
    @Override
    public List<AdvertisementInfo> selectAdvertisementInfoList(AdvertisementInfo advertisementInfo)
    {
        startPage();
        List<AdvertisementInfo> advertisementInfos =advertisementInfoMapper.selectAdvertisementInfoList(advertisementInfo);
        for(AdvertisementInfo advertisementInfo1 : advertisementInfos){
            advertisementInfo1.setSetDate(StampToString(Long.valueOf(advertisementInfo1.getSetDate())));
        }
        return advertisementInfos;
    }

    /**
     * 新增广告信息
     * 
     * @param advertisementInfo 广告信息
     * @return 结果
     */
    @Override
    public int insertAdvertisementInfo(AdvertisementInfo advertisementInfo)
    {
        if(!checkNull(advertisementInfo.getMerchantId())&&!checkNull(advertisementInfo.getAdvertisementTitle())&&!checkNull(advertisementInfo.getAdvertisementPic())&&!checkNull(advertisementInfo.getSetId())){
            CooperativeMerchant cooperativeMerchant = new CooperativeMerchant();
            cooperativeMerchant.setMerchantId(advertisementInfo.getMerchantId());
            List<CooperativeMerchant> cooperativeMerchants = cooperativeMerchantMapper.selectCooperativeMerchantList(cooperativeMerchant);
            if(cooperativeMerchants.size()>0) {
                advertisementInfo.setMerchantName(cooperativeMerchants.get(0).getMerchantName());
                advertisementInfo.setAdvertisementId(getIdStamp().toString());
                advertisementInfo.setSetDate(getStamp().toString());
                return advertisementInfoMapper.insertAdvertisementInfo(advertisementInfo);
            }
        }
        return 0;
    }

    /**
     * 修改广告信息
     * 
     * @param advertisementInfo 广告信息
     * @return 结果
     */
    @Override
    public int updateAdvertisementInfo(AdvertisementInfo advertisementInfo)
    {
        if(!checkNull(advertisementInfo.getAdvertisementId())){
            if(!checkNull(advertisementInfo.getMerchantId())) {
                CooperativeMerchant cooperativeMerchant = new CooperativeMerchant();
                cooperativeMerchant.setMerchantId(advertisementInfo.getMerchantId());
                List<CooperativeMerchant> cooperativeMerchants = cooperativeMerchantMapper.selectCooperativeMerchantList(cooperativeMerchant);
                if (cooperativeMerchants.size() <1 ) {
                    return 0;
                }
                cooperativeMerchant.setSetDate(getStamp().toString());
                advertisementInfo.setMerchantName(cooperativeMerchants.get(0).getMerchantName());
            }
            return advertisementInfoMapper.updateAdvertisementInfo(advertisementInfo);
        }
        return 0;
    }

    /**
     * 批量删除广告信息
     * 
     * @param advertisementIds 需要删除的广告信息ID
     * @return 结果
     */
    @Override
    public int deleteAdvertisementInfoByIds(String[] advertisementIds)
    {
        if(advertisementIds.length>0){
            return advertisementInfoMapper.deleteAdvertisementInfoByIds(advertisementIds);
        }
        return 0;
    }

    /**
     * 删除广告信息信息
     * 
     * @param advertisementId 广告信息ID
     * @return 结果
     */
    @Override
    public int deleteAdvertisementInfoById(String advertisementId)
    {
        return advertisementInfoMapper.deleteAdvertisementInfoById(advertisementId);
    }
}
